package com.nifostasky.comic.swagger2Config;

import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.ServletContext;

/**
 * @Author WuYuanBo qq:9976797
 * @Date 2021/5/18 19:30
 * @Version 1.0
 */
@Configuration
public class SwaggerConfig {
    //本地
    //http://localhost:8090/base-service/doc.html
    //线上
    //http://11.1.1.136:26001/api/nhssp/doc.html

    @Autowired
    private ServletContext servletContext;

    private static String basePath = "";
//    private static String basePath = "";
    private static String basePackage = "com.nifostasky.comic.controller";
    private static String basePackage1 = "com.nifostasky.comic.business.controller";
    private static String title = "基础服务";
    private static String desc = "平台管理中心";
    private static String version = "1.0";

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return basePath;
                    }
                })
                .apiInfo(apiInfo())
                .select()
                .apis(Predicates.or(
                        RequestHandlerSelectors.basePackage(basePackage),
                        RequestHandlerSelectors.basePackage(basePackage1))
                )
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(desc)
                .termsOfServiceUrl("http://11.1.1.136:26001/")
                .version(version)
                .build();
    }

}
